package com.baikeyang.service.role.impl;

import com.baikeyang.common.bean.ResponseResult;
import com.baikeyang.common.constant.ExecuteConstants;
import com.baikeyang.common.model.Role;
import com.baikeyang.common.model.RoleResource;
import com.baikeyang.common.util.StringUtils;
import com.baikeyang.common.util.shiro.ShiroUtil;
import com.baikeyang.mapper.RoleMapper;
import com.baikeyang.mapper.RoleResourceMapper;
import com.baikeyang.service.base.impl.SuperServiceImpl;
import com.baikeyang.service.role.IRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * Created by lenovo on 2018/7/20.
 */
@Service(value = "roleService")
public class RoleServiceImpl extends SuperServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleResourceMapper roleResourceMapper;

    @Override
    public IPage<Role> getRolePage(Page<Role> pageParam) {
//        QueryWrapper<Role> wr = new QueryWrapper<Role>();
//        return roleMapper.selectPage(pageParam, wr);
        return roleMapper.getRolesByCurrentUserId(pageParam, ShiroUtil.getCurrentUserId());
    }

    @Override
    public ResponseResult saveRole(Role role) {
        role.setCreateTime(new Date());
        role.setCreateUser(ShiroUtil.getCurrentUserId());
        role.setUpdateTime(new Date());
        role.setUpdateUser(ShiroUtil.getCurrentUserId());
        Integer flag = roleMapper.insert(role);
        if (flag > 0) {
            return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
        }
        return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
    }

    @Override
    public ResponseResult update(Role role) {
        role.setUpdateTime(new Date());
        role.setUpdateUser(ShiroUtil.getCurrentUserId());
        Integer flag = roleMapper.updateById(role);
        if (flag > 0) {
            return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
        }
        return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
    }

    @Override
    public ResponseResult delete(Integer id) {
        int flag = roleMapper.deleteById(id);
        if(flag > 0){
            return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
        }
        return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
    }

    @Override
    public ResponseResult delete(String ids) {
        if(StringUtils.isNotEmpty(ids)){
            List<String> idList = Arrays.asList(ids.split(","));
            QueryWrapper<Role> wrapper = new QueryWrapper<Role>();
            wrapper.in("id", idList);
            int flag = roleMapper.delete(wrapper);
            if(flag > 0 && flag == idList.size()){
                QueryWrapper<RoleResource> roleResourceWrapper = new QueryWrapper<RoleResource>();
                roleResourceWrapper.in("role_id", idList);
                roleResourceMapper.delete(roleResourceWrapper);
                return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
            }
        }
        return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
    }

    @Override
    public ResponseResult saveRoleResource(RoleResource roleResource) {
        int count = 0;
        if(null != roleResource && StringUtils.isNotEmpty(roleResource.getResources())){
            QueryWrapper<RoleResource> wrapper = new QueryWrapper<RoleResource>();
            wrapper.eq("role_id", roleResource.getRoleId());
            roleResourceMapper.delete(wrapper);
            List<String> strings = Arrays.asList(roleResource.getResources().split(","));
            for(String resource : strings){
                roleResource.setResourceId(new Long(resource));
                int flag = roleResourceMapper.insert(roleResource);
                if(flag > 0){
                    count = count + 1;
                }
            }
        }
        return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200, count);
    }

    @Override
    public ResponseResult getAllRoles() {
        QueryWrapper<Role> wrapper = new QueryWrapper<Role>();
        List<Role> roles = roleMapper.selectList(wrapper);
        return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200, roles);
    }

    @Override
    public Set<String> getResourceByRoleIds(Set<String> roles) {
        Set<String> resourceIds = new HashSet<String>();
        QueryWrapper<RoleResource> wrapper = new QueryWrapper<RoleResource>();
        List<RoleResource> roleResources = new ArrayList<RoleResource>();
        for (String roleId : roles) {
            wrapper.eq("role_id", roleId);
            roleResources.addAll(roleResourceMapper.selectList(wrapper));
        }
        if(!CollectionUtils.isEmpty(roleResources)){
            for(RoleResource roleResource : roleResources){
                resourceIds.add(roleResource.getResourceId().toString());
            }
        }
        return resourceIds;
    }

    @Override
    public ResponseResult getResourceByRoleId(Integer id) {
        QueryWrapper<RoleResource> wrapper = new QueryWrapper<RoleResource>();
        new ArrayList<RoleResource>();
        wrapper.eq("role_id", id);
        List<RoleResource> roleResources = roleResourceMapper.selectList(wrapper);
        return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200, roleResources);
    }
}
